// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable restore
namespace Elastic.Clients.Elasticsearch.TransformManagement
{
	internal sealed class PivotConverter : JsonConverter<Pivot>
	{
		public override Pivot Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
		{
			if (reader.TokenType != JsonTokenType.StartObject)
				throw new JsonException("Unexpected JSON detected.");
			var variant = new Pivot();
			while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
			{
				if (reader.TokenType == JsonTokenType.PropertyName)
				{
					var property = reader.GetString();
					if (property == "aggregations" || property == "aggs")
					{
						variant.Aggregations = JsonSerializer.Deserialize<Dictionary<string, Elastic.Clients.Elasticsearch.Aggregations.AggregationContainer>?>(ref reader, options);
						continue;
					}

					if (property == "group_by")
					{
						variant.GroupBy = JsonSerializer.Deserialize<Dictionary<string, Elastic.Clients.Elasticsearch.TransformManagement.PivotGroupByContainer>?>(ref reader, options);
						continue;
					}
				}
			}

			return variant;
		}

		public override void Write(Utf8JsonWriter writer, Pivot value, JsonSerializerOptions options)
		{
			writer.WriteStartObject();
			if (value.Aggregations is not null)
			{
				writer.WritePropertyName("aggregations");
				JsonSerializer.Serialize(writer, value.Aggregations, options);
			}

			if (value.GroupBy is not null)
			{
				writer.WritePropertyName("group_by");
				JsonSerializer.Serialize(writer, value.GroupBy, options);
			}

			writer.WriteEndObject();
		}
	}

	[JsonConverter(typeof(PivotConverter))]
	public sealed partial class Pivot
	{
		public Dictionary<string, Elastic.Clients.Elasticsearch.Aggregations.AggregationContainer>? Aggregations { get; set; }

		public Dictionary<string, Elastic.Clients.Elasticsearch.TransformManagement.PivotGroupByContainer>? GroupBy { get; set; }
	}

	public sealed partial class PivotDescriptor : SerializableDescriptorBase<PivotDescriptor>
	{
		internal PivotDescriptor(Action<PivotDescriptor> configure) => configure.Invoke(this);
		public PivotDescriptor() : base()
		{
		}

		private Dictionary<string, Elastic.Clients.Elasticsearch.Aggregations.AggregationContainer>? AggregationsValue { get; set; }

		private Dictionary<string, Elastic.Clients.Elasticsearch.TransformManagement.PivotGroupByContainer>? GroupByValue { get; set; }

		public PivotDescriptor Aggregations(Func<FluentDictionary<string, Elastic.Clients.Elasticsearch.Aggregations.AggregationContainer>, FluentDictionary<string, Elastic.Clients.Elasticsearch.Aggregations.AggregationContainer>> selector)
		{
			AggregationsValue = selector?.Invoke(new FluentDictionary<string, Elastic.Clients.Elasticsearch.Aggregations.AggregationContainer>());
			return Self;
		}

		public PivotDescriptor GroupBy(Func<FluentDictionary<string, Elastic.Clients.Elasticsearch.TransformManagement.PivotGroupByContainer>, FluentDictionary<string, Elastic.Clients.Elasticsearch.TransformManagement.PivotGroupByContainer>> selector)
		{
			GroupByValue = selector?.Invoke(new FluentDictionary<string, Elastic.Clients.Elasticsearch.TransformManagement.PivotGroupByContainer>());
			return Self;
		}

		protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
		{
			writer.WriteStartObject();
			if (AggregationsValue is not null)
			{
				writer.WritePropertyName("aggregations");
				JsonSerializer.Serialize(writer, AggregationsValue, options);
			}

			if (GroupByValue is not null)
			{
				writer.WritePropertyName("group_by");
				JsonSerializer.Serialize(writer, GroupByValue, options);
			}

			writer.WriteEndObject();
		}
	}
}